package com.yoomiito.app.ui.songduoduo.freegoods

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.GoodsInfo
import com.yoomiito.app.model.sdd.SddFreeGoodsData
import com.yoomiito.app.ui.songduoduo.SendMoreActivity
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.Spacing2Decoration
import kotlinx.android.synthetic.main.act_status_free_goods.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 申请领取赠品状态
 */
class StatusOfFreeGoodsActivity : BaseActivity<StatusOfFreeGoodsPresenter>() {

    companion object {
        fun start(context: Context, data: SddFreeGoodsData) {
            val intent = Intent(context, StatusOfFreeGoodsActivity::class.java)
            intent.putExtra(Const.Common.Data, data)
            context.startActivity(intent)
        }
    }

    private lateinit var data: SddFreeGoodsData
    private val mAdapter = RecommendGoodsAdapter(null)
    private var mRecommendTitle: ConstraintLayout? = null

    override fun getLayoutId(): Int = R.layout.act_status_free_goods

    override fun newP(): StatusOfFreeGoodsPresenter = StatusOfFreeGoodsPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        data = intent.getSerializableExtra(Const.Common.Data) as SddFreeGoodsData
        iv_back_left.setOnClickListener { finish() }
        tv_center.text = "赠品列表"

        initRcy()
        //TODO 暂时没有推送订单type
//        when(data.type){
//            2,5,6->{
//                orderDetail.visibility = View.VISIBLE
//            }
//        }
//        orderDetail.setOnClickListener {
//            Router.newIntent(context)
//                    .putString(Constant.ID, data.orderId)
//                    .putInt(Constant.TYPE, 7)
//                    .putInt(Constant.KEY, 8)
//                    .to(OrderInfoActivity::class.java)
//                    .launch()
//        }
        giveFriend.visibility = View.GONE
        showLoading()
        requestData()
//        giveFriend.background = ColorUtils.gradientAndRadius("#FFFF035B", "#FFFF246C", DensityUtil.dp2px(24f).toFloat())
//        giveFriend.visibility = if (data.type == 2) View.VISIBLE else View.GONE
//        giveFriend.setOnClickListener {
//            RouterUtils.toMain(context,2)
//        }
    }

    private fun initRcy() {
        val headerView = LayoutInflater.from(this).inflate(R.layout.view_sdd_apply_free_goods_status, null, false)
        headerView.findViewById<ImageView>(R.id.userPhoto).let {
            ImageLoader.getInstance().into(this, data.headimgurl, it)
        }
        headerView.findViewById<TextView>(R.id.userName).text = data.nickname
        headerView.findViewById<TextView>(R.id.status).text = "已申请领取赠品，等待好友审核"
        headerView.findViewById<ImageView>(R.id.ADPic).let {
            ImageLoader.getInstance().into(this, "https://mimitao-cdn.aijiamai.com/app/song_banner@3x.png", it)
            it.visibility = if (UserInfoConfig.getUser().three_role_id.toInt() >= Type.LevelType.SDD_VIP_10) View.GONE else View.VISIBLE
            it.setOnClickListener {
                 TouchUtils.onEvent(AllEventID.ADOfStatusFreeGoodsId)
//                RouterUtils.toMain(context,1)
                SendMoreActivity.start(this)
            }
        }
        headerView.findViewById<TextView>(R.id.item_goods_info_title_tv).text = "为你推荐"
        mRecommendTitle = headerView.findViewById(R.id.item_goods_info_c)
        rcy.layoutManager = androidx.recyclerview.widget.GridLayoutManager(this, 2)
        rcy.addItemDecoration(Spacing2Decoration(DensityUtil.dp2px(9f),DensityUtil.dp2px(9f),true))
        mAdapter.addHeaderView(headerView)
        rcy.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            TouchUtils.onEvent(AllEventID.RecommendGoodsOfStatusFreeGoodsId)
            RouterUtils.toGoodsDetail(this, mAdapter.data[position].id,Type.GoodsType.TaoBao)
        }
    }

    private fun requestData() {
        p.data()
    }

    fun list(data: List<GoodsInfo>) {
        dismissLoading()
        mAdapter.setNewData(data)
        mRecommendTitle?.visibility = if (data.isEmpty()) View.GONE else View.VISIBLE
    }
}